import Vue from 'vue'
import moment from 'moment'

// Vue.filter('formatDate', function (value, ) {
  
// })
const dfilters = {
	formatDate: function(value,formatString) {
		formatString = formatString || 'YYYY-MM-DD HH:mm:ss'
		if (value) {
			return moment(value).format(formatString)
		} else if(!value){
			return '--'
		}
	},
	formatDates: function(value,type,pattern = "") {
		var format = value.replace(/-/g, "/");
		let date = new Date(format);
		let y = date.getFullYear();
		let MM = date.getMonth() + 1 + "";
		MM = parseInt(MM) < 10 ? "0" + MM : MM;
		let d = date.getDate() + "";
		d = parseInt(d) < 10 ? "0" + d : d;
		let h = date.getHours() + "";
		h = parseInt(h) < 10 ? "0" + h : h;
		let m = date.getMinutes() + "";
		m = parseInt(m) < 10 ? "0" + m : m;
		let s = date.getSeconds() + "";
		s = parseInt(s) < 10 ? "0" + s : s;
		if (pattern == "yyyy-mm-dd") {
			return `${y}.${MM}.${d}`;
		} 
		else if(type===1){
			return `${y}-${MM}`;
		}
		else if(type===2){
			return `${y}-${MM}-${d}`;
		}
		else if(type===3){
			return `${y}-${MM}-${d} ${h}:${m}:${s}`;
		}
		else if(type===4){
			return `${y}年${MM}月`;
		}
		else if(type===5){
			return `${y}年${MM}月${d}日`;
		}
		else if(type===6){
			return `${y}/${MM}`;
		}
		else if(type===7){
			return `${y}/${MM}/${d}`;
		}
		else if(type===8){
			return `${y}/${MM}/${d} ${h}:${m}:${s}`;
		}
	},
	formatDateNumber: function(value,type,pattern = "") {
		const date=new Date(value);
		const year=date.getFullYear();
		const month=date.getMonth()>9?date.getMonth()+1:`0${date.getMonth()+1}`;
		const day=date.getDate()>9?date.getDate()+1:`0${date.getDate()}`;
		const hour=date.getHours();
		const minutes=date.getMinutes();
		const seconds=date.getSeconds();
		return `${year}-${month}-${day} ${hour}:${minutes}:${seconds}`
		
	},
	formatMoney:function(number, decimals, dec_point, thousands_sep){
		decimals = 2; //这里默认设置保留两位小数，也可以注释这句采用传入的参数
		/*
		* 参数说明：
		* number：要格式化的数字
		* decimals：保留几位小数
		* dec_point：小数点符号
		* thousands_sep：千分位符号
		* */
		number = (number + '').replace(/[^0-9+-Ee.]/g, '');
		var n = !isFinite(+number) ? 0 : +number,
			prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
			sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
			dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
		var s = n.toString().split('.');
		var re = /(-?\d+)(\d{3})/;
		while (re.test(s[0])) {
			s[0] = s[0].replace(re, "$1" + sep + "$2");
		}
		if ((s[1] || '').length < prec) {
			s[1] = s[1] || '';
			s[1] += new Array(prec - s[1].length + 1).join('0');
		} else {
			s[1] = s[1].substring(0, prec); //小数点位数超出长度时截取前面的位数
		}
		return s.join(dec);
	}
}

export default dfilters;